package com.mrjy.schooldiy.bo;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.DiyArticleInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.CharSetUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.schooldiy.dao.IDiyArticleDao;
import com.mrjy.schooldiy.dto.DiyArticleDTO;
import com.mrjy.schooldiy.dto.DiyNoticeDTO;

@Service("schoolArticleBo")
public class DiyArticleBo implements IDiyArticle{
	@Resource
	private IDiyArticleDao schoolArticleDao;
	
	@Override
	public Object insertDiyArticle(DiyArticleInfo articleInfo,
			HttpSession session) {
	// TODO Auto-generated method stub
		Map< String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(loginUser == null){
			map.put("err_code",-1);
			map.put("err_msg","用户未登录");
			return map;
		}
		if(articleInfo.getSchoolId() == null){
			map.put("err_code",401);
			map.put("err_msg","学校的id不能为空");
			return map;
		}
		articleInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		articleInfo.setPublishTime(new Timestamp(System.currentTimeMillis()));
		int count = schoolArticleDao.insertDiyArticle(articleInfo);
		if(count == 1){
			map.put("err_code", 200);
			map.put("err_msg", "通知插入成功");
		}else {
			map.put("err_code", 201);
			map.put("err_msg", "通知信息插入失败");
		}
		return map;
	}

	@Override
	public Object updateDiyArticle(DiyArticleInfo articleInfo,
			HttpSession session) {
		// TODO Auto-generated method stub
		Map< String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(loginUser == null){
			map.put("err_code",-1);
			map.put("err_msg","用户未登录");
			return map;
		}
		if(articleInfo.getSchoolId() == null){
			map.put("err_code",401);
			map.put("err_msg","学校的id不能为空");
			return map;
		}
		articleInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		articleInfo.setPublishTime(new Timestamp(System.currentTimeMillis()));
		int count = schoolArticleDao.updateDiyArticle(articleInfo);
		if(count == 1){
			map.put("err_code", 200);
			map.put("err_msg", "通知插入成功");
		}else {
			map.put("err_code", 201);
			map.put("err_msg", "通知信息插入失败");
		}
		return map;
	}

	@Override
	public Object txUpdateDisableFlag(DiyArticleInfo articleInfo) {
		// TODO Auto-generated method stub
		Map< String, Object> map = new HashMap<String, Object>();
		int row = schoolArticleDao.updateDisableFlag(articleInfo);
		if(row > 0){
			map.put("err_code", 200);
			map.put("err_msg", "修改成功");
		}else {
			map.put("err_code", 201);
			map.put("err_msg", "修改失败");
		}
		return map;
	}

	@Override
	public Object queryPageForPc(DiyArticleInfo articleInfo, HttpSession session) {
		// TODO Auto-generated method stub
		Map< String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(loginUser == null){
			map.put("err_code",-1);
			map.put("err_msg","用户未登录");
			return map;
		}
		int total = schoolArticleDao.countPageForPc(articleInfo);
		if(total > 0){
			List<DiyArticleDTO> queryList = schoolArticleDao.queryPageForPc(articleInfo); 
			if(queryList != null){
				map.put("err_code",200);
				map.put("err_msg","查询成功有数据");
				map.put("rows", queryList);
				map.put("total", total);
			}else {
				map.put("err_code",200);
				map.put("err_msg","查询成功无数据");
				map.put("rows", null);
			}
		}else {
			map.put("err_code",200);
			map.put("err_msg","查询成功无数据");
			map.put("rows", null);
			map.put("total", 0);
		}
		
		return map;
	}

	@Override
	public Object queryPageList(DiyArticleInfo articleInfo, HttpSession session) {
		// TODO Auto-generated method stub
		Map< String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(loginUser == null){
			map.put("err_code",-1);
			map.put("err_msg","用户未登录");
			return map;
		}
		List<DiyArticleDTO> queryList = schoolArticleDao.queryPageList(articleInfo); 
		if(queryList != null && queryList.size()>0){
			map.put("err_code",200);
			map.put("err_msg","查询成功有数据");
			map.put("rows", queryList);
		}else {
			map.put("err_code",200);
			map.put("err_msg","查询成功无数据");
			map.put("rows", null);
		}
		
		return map;
	}

	@Override
	public Object queryArticleById(DiyArticleInfo articleInfo,
			HttpSession session) {
		// TODO Auto-generated method stub
		Map< String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(loginUser == null){
			map.put("err_code",-1);
			map.put("err_msg","用户未登录");
			return map;
		}
		DiyArticleDTO diyArticleDTO = schoolArticleDao.queryArticleById(articleInfo); 
		if(diyArticleDTO != null){
			map.put("err_code",200);
			map.put("err_msg","查询成功有数据");
			map.put("rows", diyArticleDTO);
		}else {
			map.put("err_code",200);
			map.put("err_msg","查询成功无数据");
			map.put("rows", null);
		}
		
		return map;
	}

	@Override
	public Object addArticleViewNum(DiyArticleInfo articleInfo,
			HttpSession session) {
		// TODO Auto-generated method stub
		Map< String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(loginUser == null){
			map.put("err_code",-1);
			map.put("err_msg","用户未登录");
			return map;
		}
		int count = schoolArticleDao.addArticleViewNum(articleInfo.getId()); 
		if(count > 0){
			map.put("err_code",200);
			map.put("err_msg","增加成功");
		}else {
			map.put("err_code",201);
			map.put("err_msg","新加失败");
		}
		
		return map;
	}

}
